﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhSystemBLL.basicData;
using dhDataAcess.Model;
using baseSet.importExport;
using dhsystem.Main;
using FastReport;
using baseSet.power;
using dhDataAcess.dataAcess;

namespace dhsystem.basicForm
{
    public partial class FrmDeptList : Form
    {
        deptBLL dept = new deptBLL();
        deptListModel deptRow = new deptListModel();
        public FrmDeptList()
        {
            InitializeComponent();
        }
        //新增按钮，显示新增数据组件
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = true;
            if (lblState.Text == "编辑")
            {
                MessageBox.Show("当前在编辑数据状态，请先保存编辑数据或者取消再进行编辑");
                return;
            }
            lblState.Text = "新增";
            if (groupAdd.Visible == false)
            {
                groupAdd.Visible = true;
                groupList.Width = groupList.Width - 288;
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
            //FrmDetpAdd deptAdd = new FrmDetpAdd();
            //deptAdd.ShowDialog();
            textdeptNum.Focus();
        }
        //打开窗口时显示列表数据并且隐藏新增数据组件
        private void FrmDeptList_Load(object sender, EventArgs e)
        {
            bindSourceDept.DataSource = dept.getdeptListAll();
            dGridDetpList.DataSource = bindSourceDept;
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;

            DcolumnSet.colSet(moduleInfo.deptList, dGridDetpList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridDetpList.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.deptList );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }

        }
        //工具栏退出按钮
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (lblState.Text == "新增")
            {

                try
                {
                    DialogResult result = MessageBox.Show("是否新增部门资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        deptRow.deptname = textdeptName.Text;
                        deptRow.deptnum = textdeptNum.Text;
                        deptRow.createuser = textcreateuser.Text;
                        deptRow.createdate = DateTime.Now;
                        deptRow.updatedate = DateTime.Now;
                        deptRow.updateuser = textupdateuser.Text;
                        deptRow.mark = textmark.Text;
                        dept.deptListInsert (deptRow);
                        bindSourceDept.DataSource = dept.getdeptListAll();
                        if (checkAdd.Checked == true)
                        {
                            lblState.Text = "新增";
                        }
                        else
                        {
                            lblState.Text = "正常";
                        } 
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

            if (lblState.Text == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否更改部门资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        deptRow.deptname = textdeptName.Text;
                        deptRow.deptnum = textdeptNum.Text;
                        deptRow.createuser = textcreateuser.Text;
                        deptRow.createdate = DateTime.Now;
                        deptRow.updatedate = DateTime.Now;
                        deptRow.updateuser = textupdateuser.Text;
                        deptRow.mark = textmark.Text;
                        dept.deptListUpdate (deptRow);
                        bindSourceDept.DataSource = dept.getdeptListAll();
                        lblState.Text = "正常";
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.deptList );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridDetpList.DataSource = query.rTable;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmImport import = new FrmImport(moduleInfo.deptList );
            import.ShowDialog();
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindSourceDept .DataSource =dept .getdeptListAll ("%" +textdeptNumQuery .Text  + "%");
                dGridDetpList.DataSource = bindSourceDept;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //取消按钮，隐藏新增数据组件
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            lblState.Text = "正常";
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = false;
            try
            {
                if (lblState.Text == "新增")
                {
                    MessageBox.Show("当前在新增数据状态，请先保存新增数据或者取消再进行编辑");
                    return;
                }

                lblState.Text = "编辑";

                if (groupAdd.Visible == false)
                {
                    groupAdd.Visible = true;
                    groupList.Width = groupList.Width - 288;
                    groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                }

                DataGridViewRow currentRow = dGridDetpList.CurrentRow;
                textdeptNum.Text = currentRow.Cells["deptNum"].Value.ToString();
                textdeptName.Text = currentRow.Cells["deptName"].Value.ToString();
                textcreateuser.Text = currentRow.Cells["createUser"].Value.ToString();
                textupdateuser.Text = currentRow.Cells["updateUser"].Value.ToString();
                textmark.Text = currentRow.Cells["mark"].Value.ToString();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string deptNum = dGridDetpList.Rows[dGridDetpList.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除部门编号为" + deptNum + "的部门信息！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                  bindSourceDept .DataSource = dept .deptListDel (deptNum);
                  dGridDetpList.DataSource = bindSourceDept;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            dGridDetpList.DataSource = dept.getdeptListAll();
        } 

        private void FrmDeptList_Resize(object sender, EventArgs e)
        {
            if (groupAdd.Visible == true)
            {
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
        }
        //datagridview第一列显示行号
        private void dGridDetpList_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void toolPrint_Click(object sender, EventArgs e)
        {
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(dept .getdeptListAll ());
          //  r.Load(@"e:\东华软件项目\报表\orderproduce.frx");
            r.RegisterData(ds);
            r.Design();
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.deptList , dGridDetpList , "部门信息");
            frm.ShowDialog();
        }
    }
}
